package com.meida.starter.rabbitmq.event;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 监听远程事件,并分发消息到业务模块消息处理器
 *
 * @author zyf
 */
@Component
public class BaseApplicationEvent implements ApplicationListener<MeidaRemoteApplicationEvent> {

    @Autowired(required = false)
    Map<String, MeidaBusEventHandler> meidaBusEventHandlerMap;

    @Override
    public void onApplicationEvent(MeidaRemoteApplicationEvent jeecgRemoteApplicationEvent) {
        EventObj eventObj = jeecgRemoteApplicationEvent.getEventObj();
        if (ObjectUtil.isNotEmpty(eventObj)) {
            //获取业务模块消息处理器
            for (MeidaBusEventHandler meidaBusEventHandler : meidaBusEventHandlerMap.values()) {
                if (meidaBusEventHandler.support(eventObj)) {
                    meidaBusEventHandler.onMessage(eventObj);
                }
            }
        }
    }

}
